package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                employeeMapper.insertRole(employee.getId(), id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRole(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRole(employee.getId());
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                employeeMapper.insertRole(employee.getId(), id);
            }
        }
    }

    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);


    }

    @Override
    public List selectByEmpId(Long id) {
        return employeeMapper.selectByEmpId(id);
    }

    @Override
    public Employee empLogin(String username, String password) {
        if (StringUtils.isEmpty(username)) {
            throw new BusinessException("用户名不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException("密码不能为空");
        }
        Employee employee = employeeMapper.selectByUser(username);
        if (employee == null || !employee.getPassword().equals(password)) {
            throw new BusinessException("账户密码错误");
        }
        return employee;

    }

    @Override
    public List<Permission> selectPermissionById(Long id) {
        return employeeMapper.selectPermissionById(id);
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("员工通讯录");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工邮箱");

        List<Employee> employees = employeeMapper.selectAll();
        System.out.println(employees);
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getEmail());


        }
        workbook.write(outputStream);
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //
        HSSFWorkbook sheets = new HSSFWorkbook(file.getInputStream());

        //获取第一页
        HSSFSheet sheetAt = sheets.getSheetAt(0);

        //获取最后一行的索引
        int lastRowNum = sheetAt.getLastRowNum();

        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheetAt.getRow(i);

            //创建对象封装文件中的字段
            Employee employee = new Employee();
            employee.setName(row.getCell(0).getStringCellValue());
            Double num = row.getCell(1).getNumericCellValue();
            employee.setAge(num.intValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            employeeMapper.insert(employee);
        }
    }


}
